﻿// -------------------------------------------------------------------------------------------------------
// LICENSE INFORMATION
//
// - This software is licensed under the MIT shared source license.
// - The "official" source code for this project is maintained at http://mtconnect.codeplex.com
//
// Copyright (c) 2010 OpenNETCF Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
// -------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace OpenNETCF.MTConnect
{
    internal class CFAdapterWatcher : IAdapterWatcher
    {
        public event EventHandler<GenericEventArgs<string>> NewDLLDetected;

#if WindowsCE
        // FSW requires AYGSHELL, which isn't in my target
        private OpenNETCF.IO.FileSystemMonitor m_fsw;

        public CFAdapterWatcher(string path)
        {
            m_fsw = new OpenNETCF.IO.FileSystemMonitor(path);
            m_fsw.IncludeSubdirectories = false;
            m_fsw.Filter = "*.dll";
            m_fsw.EnableRaisingEvents = true;

            // Do *not* use "Created" here as the event will fire before the file copy
            // is complete and the resulting Assembly.Load (in AdapterService) will fail
            m_fsw.Changed += new OpenNETCF.IO.FileSystemEventHandler(m_fsw_Created);
        }

        void m_fsw_Created(object sender, OpenNETCF.IO.FileSystemEventArgs e)
        {
            // watch for new DLLs
            if (string.Compare(Path.GetExtension(e.Name), ".dll", true) == 0)
            {
                NewDLLDetected.Fire(this, new GenericEventArgs<string>(e.FullPath));
            }
        }
#else
        public CFAdapterWatcher(string path)
        {
            throw new PlatformNotSupportedException();
        }
#endif
    }
}
